/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.hutech09.jsale.web.beans;

import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import org.hutech09.jsale.bsn.ICustomerManager;
import org.hutech09.jsale.bsn.exception.ItemExistsException;
import org.hutech09.jsale.bsn.exception.ItemNotFoundException;
import org.hutech09.jsale.entity.Customer;

/**
 *
 * @author Nam
 */
@ManagedBean
@RequestScoped
public class CustomerCtrl {

    @EJB
    private ICustomerManager customerManager;

    private Customer customer = new Customer();

    public CustomerCtrl() {
    }

    public ICustomerManager getCustomerManager() {
        return customerManager;
    }

    public void setCustomerManager(ICustomerManager customerManager) {
        this.customerManager = customerManager;
    }

    public Customer getCustomer() {
        return customer;
    }

    public void setCustomer(Customer customer) {
        this.customer = customer;
    }

    public String create() {
        try {
            customerManager.create(customer);
        } catch (ItemExistsException ex) {
            try {
                customer = customerManager.update(customer);
            } catch (ItemNotFoundException ex1) {
            }
            return "create.xhtml";
        } catch (Exception e) {
            return "create.xhtml";
        }
        return "success.xhtml";
    }
    
    public String delete(){
        try {
            customerManager.delete(customer);
        } catch (ItemNotFoundException ex) {
            return "create.xhtml";
        }
        return "success.xhtml";
    }
}
